import { Component, Vue, Prop } from 'vue-property-decorator';
import { Badge, Avatar, Dropdown, Menu, Modal } from 'ant-design-vue';
import ChangePassword from "./change-password/index.vue";
import ChangeUserinfo from "./change-userinfo/index.vue";
@Component({
  components: {
    'a-badge': Badge,
    'a-avatar': Avatar,
    'a-dropdown': Dropdown,
    'a-menu': Menu,
    'a-menu-item': Menu.Item,
    'p-change-password': ChangePassword,
    'p-change-userinfo': ChangeUserinfo
  }
})
export default class AppLoginInfo extends Vue {
  @Prop({
    required: false,
    default: '',
  }) name!: string
  /** 用户头像地址 */
  src: string = '/assets/images/desktop/note.png';
  /** 用户名称 */
  userName: string = 'nobody';
  /** 修改用户信息弹窗 */
  userinfoVisible: boolean = false;
  /** 修改密码弹窗 */
  passwordVisible: boolean = false;

  get isNoticePage() {
    return this.$route.fullPath.indexOf('/notification/') > -1;
  }

  created() {
    const sysConfigMap = JSON.parse(sessionStorage.getItem('sysConfigMap') as any);
    const currentHumanInfo = JSON.parse(sessionStorage.getItem('currentHumanInfo') as any);
    this.src = "/assets/images/desktop/note.png";
    if (currentHumanInfo) {
      this.userName = currentHumanInfo.humanName;
      // this.getAvatar(currentHumanInfo.humanId);
    }
  }

  async getAvatar(humanId: number) {
    const data = await Vue.prototype.$axios.get('/media/api/avatar?id=' + humanId);
    if (data !== null && data !== '' && typeof data === 'string') {
      this.setAvatar(data);
    }
  }

  setAvatar(url: string) {
    this.src = 'data:image/jpeg;base64,' + url;
  }

  signOut() {
    Modal.confirm({
      title: '确认',
      content: '您确定要退出登录吗?',
      centered: true,
      okText: '确定',
      cancelText: '取消',
      onOk: () => {
        sessionStorage.clear();
        this.$router.push('/login');
      }
    });
  }
}